﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;
using Reborn_WorldServer.App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Potion_Teleport : IConsumable
    {
        private Character character;

        public bool MeetsConsumeRequirements(Character character, Loot loot)
        {
            return true;
        }

        public void Consume(Character myCharacter, Loot lootitem)
        {
            character = myCharacter;

            LockItem lockItem = Managers.GameHandler.GetLockItem(myCharacter.MapID, myCharacter.Instance);
            lock (lockItem.LockObj)
            {
                
                //List<Character> CharactersWithin4 = new List<Character>();
                foreach (MapItem mapitem in myCharacter.currentMapItem.mapItemsWithin8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character ctr = (Character)creaturebase;
                            ctr.PlaySound("DrinkBottle", myCharacter.MapX, myCharacter.MapY);
                        }
                    }
                }
            }
            if (lootitem.Name.IndexOf("Renatus") > -1)
            {
                string message = Managers.GameHandler.Teleport(myCharacter, 11, 44, 9, preServerTransferMethod);
                if (message != "" && message != "true")
                    myCharacter.SendMessage(message, "server");
            }
            else if (lootitem.Name.IndexOf("Lake Shire") > -1)
            {
                string message = Managers.GameHandler.Teleport(myCharacter, 6, 9, 18, preServerTransferMethod);
                if (message != "" && message != "true")
                    myCharacter.SendMessage(message, "server");
            }
        }

        private void preServerTransferMethod()
        {
            //Try to stop the round timer, this will only return true, if it was running, and was stopped
            //This means they are perma busy until they hit the other server,
            //If we return false, they are not busy due to lag or something, set them busy before transfer.
            if (character.tryStopRoundTimer() == false)
                character.SetBusy(true);
        }
    }
}
